package com.cjr.ddp.controller;

import com.cjr.ddp.entity.ReturnTargetData;
import com.cjr.ddp.service.BigScreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * @author cjr
 * @date Created in 2020-03-08 14:37
 * @description 大屏数据控制层
 * @modified By
 */

@RestController
@RequestMapping("/bigScreen")
public class BigScreenController {

    @Autowired
    BigScreenService bigScreenService;

    @Autowired
    RestTemplate restTemplate;


    /**
     * @param
     * @return java.util.List<com.cjr.ddp.entity.ReturnTargetData>
     * @throws
     * @author cjr
     * @date 2020-03-08 15:35
     * @description 销售量分析
     */
    @RequestMapping("/salesVolumeAnalysis")
    public List<ReturnTargetData> salesVolumeAnalysis() {
        String param = " YEAR (a.target_time) = YEAR (date_sub(now(), INTERVAL 1 YEAR)) AND b.id IN (40, 37, 39, 38)";
        return bigScreenService.selectTargetDataList(param);
    }

    /**
     * @param
     * @return java.util.List<com.cjr.ddp.entity.ReturnTargetData>
     * @throws
     * @author cjr
     * @date 2020-03-08 15:35
     * @description 净产量分析
     */
    @RequestMapping("/netOutputAnalysis")
    public List<ReturnTargetData> netOutputAnalysis() {
        String param = " a.target_time BETWEEN DATE_SUB(NOW(), INTERVAL 10 YEAR) AND NOW() AND b.id = 3 ORDER BY a.target_time";
        return bigScreenService.selectTargetDataList(param);
    }


    /**
     * @param
     * @return java.util.List<com.cjr.ddp.entity.ReturnTargetData>
     * @throws
     * @author cjr
     * @date 2020-03-08 15:35
     * @description 预估产量分析
     */
    @RequestMapping("/estimatedProductionAnalysis")
    public List<ReturnTargetData> estimatedProductionAnalysis() {
        String param = " a.target_time BETWEEN DATE_SUB(NOW(), INTERVAL 10 YEAR) AND NOW() AND b.id = 2 ORDER BY a.target_time";
        return bigScreenService.selectTargetDataList(param);
    }


    /**
     * @param
     * @return java.util.List<com.cjr.ddp.entity.ReturnTargetData>
     * @throws
     * @author cjr
     * @date 2020-03-08 15:35
     * @description 降水量分析
     */
    @RequestMapping("/precipitationAnalysis")
    public List<ReturnTargetData> precipitationAnalysis() {
        String param = " a.target_time BETWEEN DATE_SUB(NOW(), INTERVAL 10 YEAR) AND NOW() AND b.id = 14 ORDER BY a.target_time";
        return bigScreenService.selectTargetDataList(param);
    }


    /**
     * @param
     * @return java.util.List<com.cjr.ddp.entity.ReturnTargetData>
     * @throws
     * @author cjr
     * @date 2020-03-08 15:35
     * @description 施肥量分析
     */
    @RequestMapping("/fertilizationAnalysis")
    public List<ReturnTargetData> fertilizationAnalysis() {
        String param = " a.target_time BETWEEN DATE_SUB(NOW(), INTERVAL 10 YEAR) AND NOW() AND b.id = 15 ORDER BY a.target_time";
        return bigScreenService.selectTargetDataList(param);
    }


    /**
     * @param
     * @return java.util.List<com.cjr.ddp.entity.ReturnTargetData>
     * @throws
     * @author cjr
     * @date 2020-03-08 15:35
     * @description 重要指标分析
     */
    @RequestMapping("/importTargetAnalysis")
    public List<ReturnTargetData> importTargetAnalysis() {
        String param = " YEAR (a.target_time) = YEAR (date_sub(now(), INTERVAL 1 YEAR)) AND b.id IN (34, 33, 32,31,30,13,12,10,29,28)";
        return bigScreenService.selectTargetDataList(param);
    }


    /**
     * @param
     * @return java.util.List<com.cjr.ddp.entity.ReturnTargetData>
     * @throws
     * @author cjr
     * @date 2020-03-08 15:35
     * @description 成活率死亡率分析
     */
    @RequestMapping("/ratioAnalysis")
    public List<Map<String, Object>> ratioAnalysis() {
        return bigScreenService.selectLiveAndDeath();
    }

    /**
     * @param
     * @return java.lang.String
     * @throws
     * @author cjr
     * @date 2020-03-08 18:09
     * @description 天气查询接口
     */
    @RequestMapping("/queryWeather")
    public String queryWeather() {
        String apiURL = "http://wthrcdn.etouch.cn/weather_mini?city=" + "叶城县";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiURL, String.class);

        if (200 == responseEntity.getStatusCodeValue()) {
            return responseEntity.getBody();
        } else {
            return "error with code : " + responseEntity.getStatusCodeValue();
        }
    }

    /**
     * @param
     * @return java.util.List<com.cjr.ddp.entity.ReturnTargetData>
     * @throws
     * @author cjr
     * @date 2020-03-08 15:35
     * @description 顶部单项指标分析
     */
    @RequestMapping("/topAnalysis")
    public List<ReturnTargetData> topAnalysis() {
        String param = " YEAR (a.target_time) = YEAR (date_sub(now(), INTERVAL 1 YEAR)) AND b.id IN (27,28,29)";
        return bigScreenService.selectTargetDataList(param);
    }

}
